package Tools;

import java.io.File;
import java.util.*;

/**
This class implements base functions of file manager
@author Ivan
 */
public class TBrowser {

    private File root;

    public TBrowser() {
        root = null;
    }

    /**
     * This method displays the content of the directory which specified as parameter.
     */
    public void showFiles(String directory) throws NullPointerException, SecurityException {

        //Initialization of root directory
        root = new File(directory);

        //Print result
        for (File child : root.listFiles()) {
            System.out.print(child.getName() + "\n");
        }
    }

    /**
     * This method  displays the directory tree and theirs content in console window.
     * Name of initial directory transfered as input parameter.
     */
    public void showTreeOfDirectories(String directory) throws NullPointerException, SecurityException {

        //Initialization of root directory
        root = new File(directory);

        //List of all files which were found in specified directory
        List<File> nodes = new ArrayList<File>(0);
        nodes = Arrays.asList(root.listFiles());

        String tree = null;
        ///Display of tree
        for (File file : nodes) {
            tree = "├────" + file.getName() + "\n";
            System.out.print(tree);

            if (file.isDirectory()) {
                depthSearchPrint(file, "|\t");
            }
        }
    }

    //Recursive method search in depth with display of tree
    private void depthSearchPrint(File root, String strTab) {

        String strClildNode = strTab + "└────";
        String fileName = null;


        for (File file : root.listFiles()) {

            fileName = file.getName();

            System.out.print(strClildNode + fileName + "\n");

            if ((file.listFiles() != null) && (file.listFiles().length != 0)) {
                strTab += "\t";
                depthSearchPrint(file, strTab);
            }
        }
    }

    //Recursive method search in depth
    private void depthSearch(File root, ArrayList<File> AllFiles) {
        for (File node : root.listFiles()) {
            AllFiles.add(node);
            if ((node.listFiles() != null) && (node.listFiles().length != 0)) {
                depthSearch(node, AllFiles);
            }
        }

    }

    /**
     *This method displays the content of the directory with extended information using searching mask.
     *Directory and mask of search specified as parameters this methods.
     */
    public void showFiles(String directory, MaskCaseSensitive filter) throws NullPointerException, SecurityException {

        //Initialization of root directory
        root = new File(directory);

        //List of all files which were found in specified directory
        ArrayList<File> all_found_files = new ArrayList<File>(0);

        //List of files which satisfy requirement mask of searching
        ArrayList<File> processed_files = new ArrayList<File>(0);

        //Search all files
        depthSearch(root, all_found_files);

        // Verification on compliance
        for (File file : all_found_files) {
            if (filter.match(file)) {
                processed_files.add(file);
            }
        }

        //Print result
        System.out.print("(Amount of found files : " + processed_files.size() + ") \n");
        for (File file : processed_files) {
            System.out.print("Name of file : " + file.getName()
                    + " ; full path : " + file.getAbsolutePath()
                    + " ; date of last change : " + new Date(file.lastModified()).toString() + " ; \n");
        }

    }
}
